﻿using PowerHub.MES.DTO.Shopfloor.QueryDTO;
using PowerHub.MES.EFBase;
using PowerHub.MES.Entities.Shopfloor;
using PowerHub.MES.Entities;
using PowerHub.MES.IRepository.IShopfloor;
using PowerHub.MES.ReturnEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PowerHub.MES.Entities.Crew;
using PowerHub.MES.IRepository.ICrew;
using Microsoft.EntityFrameworkCore;
using PowerHub.MES.DTO.Crew.QueryDto;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
using PowerHub.MES.Entities.BasicData;

namespace PowerHub.MES.Repository.Crew
{
    /// <summary>
    /// 班组明细数据实现层
    /// </summary>
    public class Crew_TeamBillDetialRepository : BaseRepository<Crew_TeamBillDetial>, ICrew_TeamBillDetialRepository
    {
        private PowerHubMesContext _repositorySys;
        public Crew_TeamBillDetialRepository(PowerHubMesContext repositorySys) : base(repositorySys)
        {
            _repositorySys = repositorySys;
        }


        /// <summary>
        /// 添加班组明细
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> CreateCrew_TeamBillDetialAsync(Crew_TeamBillDetial query)
        {
            var result = new OperationResult<bool>();
            if (AddEntity(query) > 0)
            {
                result.Data = true;
            }
            else
            {
                result.Data = false;

            }

            return result;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> DeletionCrew_TeamBillDetialAsync(List<string>? ids)
        {
            var result = new OperationResult<bool>();

            var Departments = GetEntitys().AsNoTracking().Where(p => ids.Contains(p.Id)).ToList();
            Departments.ForEach(w => w.IsDel = 1);
            result.Data = UpdataEntityRange(Departments) > 0 ? true : false;

            return result;
        }


        /// <summary>
        /// 获取班组明细列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<Crew_TeamBillDetial>> GetCrew_TeamBillDetialListAsync(string id)
        {
           return await GetEntitys().AsNoTracking().Where(c=>c.TeamId.Equals(id)).Where(c=>c.IsDel==0).ToListAsync();
        }

        /// <summary>
        /// 获取班组明细列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationPageResult<List<BasicData_Userinfo>>> GetCrew_TeamBillDetialPageListAsync(QueryCrew_TeamBillDetialDTO query)
        {
            var result = new OperationPageResult<List<BasicData_Userinfo>>();

            var currentQuery = GetEntitys().AsNoTracking()
                                             .Include(p => p.User)
                                             .Include(p => p.User.Department)
                                             .Include(p => p.User.Roleinfo)
                                            .Where(p => p.IsDel == 0).Where(p => p.TeamId
                                            .Equals(query.TeamId));

            result.PageIndex = query.PageIndex;
            result.PageSize = query.PageSize;
            result.TotalCount = await currentQuery.CountAsync();

            if (result.TotalCount > 0)
            {
                result.Data= await currentQuery.OrderByDescending(g => g.Createtime).Skip((query.PageIndex - 1) * query.PageSize).Take(query.PageSize).Select(g => g.User).ToListAsync();
            }

            return result;
        }

        /// <summary>
        /// 更新班组明细
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> UpdateCrew_TeamBillDetialAsync(List<Crew_TeamBillDetial> addcrew_TeamBillDetials, List<Crew_TeamBillDetial> deletecrew_TeamBillDetials)
        {
            var result = new OperationResult<bool>();
            var tran = _repositorySys.Database.BeginTransaction();
            try
            {
                //新增
                AddEntitieRange(addcrew_TeamBillDetials);

                //删除
                DeleteEntityRange(deletecrew_TeamBillDetials);
                result.Data = true;

                tran.Commit(); //提交
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Data = false;
                tran.Rollback(); //回滚
            }

            return result;
        }
    }
}
